/*
 * Created on 17-ene-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package hotel.ejb;

import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import javax.ejb.CreateException;

import hotel.interfaces.*;
import java.util.*;

/**
 * @ejb.bean name="ClientFacade"
 *           display-name="Name for ClientFacade"
 *           description="Description for ClientFacade"
 *           jndi-name="ejb/ClientFacade"
 *           type="Stateless"
 *           view-type="both"
 * 
 * @ejb.util generate="physical"
 */
public class ClientFacadeBean implements SessionBean {

	ClientLocalHome clientHome = null;

	/**
	 * 
	 */
	public ClientFacadeBean() {
		super();
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbActivate()
	 */
	public void ejbActivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbPassivate()
	 */
	public void ejbPassivate() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#ejbRemove()
	 */
	public void ejbRemove() throws EJBException, RemoteException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
	 */
	public void setSessionContext(SessionContext ctx)
		throws EJBException,
		RemoteException {
		// TODO Auto-generated method stub

	}

	/**
	 * Default create method
	 * 
	 * @throws CreateException
	 * @ejb.create-method
	 */
	public void ejbCreate() throws CreateException {
		// TODO Auto-generated method stub
		try {
			clientHome = ClientUtil.getLocalHome();
		} catch (Exception e) {
			throw new CreateException(e.getMessage());
		}
	}

	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void setNom(Integer DNI, String Nom) throws FinderException {
		// TODO Auto-generated method stub
		ClientLocal c = clientHome.findByPrimaryKey(new ClientPK(DNI));
		c.setNom(Nom);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public String getNom(Integer DNI) throws FinderException {
		// TODO Auto-generated method stub
		ClientLocal c = clientHome.findByPrimaryKey(new ClientPK(DNI));
		return c.getNom();
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void alta(Integer DNI, String nom) throws CreateException {
		// TODO Auto-generated method stub
		ClientValue c = new ClientValue();
		c.setDNI(DNI);
		c.setNom(nom);
		clientHome.create(c);
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public void baixa(Integer DNI) throws FinderException, RemoveException {
		// TODO Auto-generated method stub
		ClientLocal c = clientHome.findByPrimaryKey(new ClientPK(DNI));
		c.remove();
	}
	
	/**
	 * Business method
	 * @ejb.interface-method  view-type = "both"
	 */
	public Collection llistat() throws FinderException {
		// TODO Auto-generated method stub
		Collection llistat = new Vector();
		
		Collection c = clientHome.findAll();
		Iterator it = c.iterator();
		while(it.hasNext()) {
			ClientLocal client = (ClientLocal)it.next();
			// Llistat de ClientValue
			ClientValue vClient = new ClientValue();
			vClient.setDNI(client.getDNI());
			vClient.setNom(client.getNom());
			llistat.add(vClient);
		}
		return llistat;
	}
}